<template >
    <div>
        <h1>用户添加</h1>
    </div>
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"  class="demo-ruleForm"
      :size="formSize"
      status-icon
    >
      <el-form-item label="用户姓名" prop="UserName">
        <el-input v-model="ruleForm.UserName" />
      </el-form-item>
      <el-form-item label="性别" prop="Sex">
        <el-input v-model="ruleForm.Sex" />
      </el-form-item>
      <el-form-item label="电话" prop="Tel">
        <el-input v-model="ruleForm.Tel" />
      </el-form-item>
      <el-form-item label="登录姓名" prop="LoginName">
        <el-input v-model="ruleForm.LoginName" />
      </el-form-item>
      <el-form-item label="登录密码" prop="LoginPwd">
        <el-input v-model="ruleForm.LoginPwd" type="password" show-password/>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          添加
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </template>
  
  <script lang="ts" setup>
  import { reactive, ref,onMounted } from 'vue'
  import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
  import axios from 'axios'
  import { useRouter,useRoute } from 'vue-router'
  import { ElMessage } from 'element-plus'

  const router=useRouter();
  const route=useRoute();
  
  
  interface RuleForm {
    UserName: string
    Sex: string
    Tel: string
    LoginName: string
    LoginPwd: string
  }
  
  const formSize = ref<ComponentSize>('default')
  const ruleFormRef = ref<FormInstance>()
  const ruleForm = reactive<RuleForm>({
    UserName: '',
    Sex: '',
    Tel: '',
    LoginName: '',
    LoginPwd: '',

  })
  
  const locationOptions = ['Home', 'Company', 'School']
  
  const rules = reactive<FormRules<RuleForm>>({
    UserName: [
      { required: true, message: '请输入用户名称', trigger: 'blur' },
      //{ min: 3, max: 5, message: 'Length should be 3 to 5', trigger: 'blur' },
    ],
    Sex: [
      {
        required: true,
        message: '请输入性别',
        trigger: 'blur',
      },
    ],
    Tel: [
      {
        required: true,
        message: '请输入电话',
        trigger: 'blur',
      },
      { min: 11, max: 11, message: '电话只能是11位', trigger: 'blur' },
    ],
    LoginName: [
      {
        
        required: true,
        message: '请输入登录名称',
        trigger: 'blur',
      },
    ],
    LoginPwd: [
      {
       
        required: true,
        message: '请输入密码',
        trigger: 'blur',
      },
      { min: 6, max: 16, message: '密码长度必须是6位或16位', trigger: 'blur' },
    ],
    
  })
  
  const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
      if (valid) {
        console.log('submit!',ruleForm)
        axios.post('/api/User/AddUserInfoAsync',ruleForm).then(res=>{
            console.log(res);
            if(res.data.code==200){
                ElMessage({
                message: '添加成功',
                type: 'success',
               })
            }else{
                ElMessage.error('添加失败')
            }
            
        }).catch(err=>{
            console.log(err);
            
        })
      } else {
        console.log('error submit!', fields)
      }
    })
  }
  
  const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
  }
  
  const options = Array.from({ length: 10000 }).map((_, idx) => ({
    value: `${idx + 1}`,
    label: `${idx + 1}`,
  }))
  </script>
  